package ricelee.demo.rxjava;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;


public class SubscribeActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar toolbar;


    private Button noParamsBtn;
    private Button onNextBtn;
    private Button onErrorBtn;

    private TextView sendTv;
    private TextView receiveTv;

    private StringBuilder sendBuilderString;
    private StringBuilder receiveBuilderString;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscribe);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Subscribe重载");
//        toolbar.setTitle("");

        init();
        initData();
    }

    private void init() {
        noParamsBtn = (Button) findViewById(R.id.btn_subscribe_no_param);
        noParamsBtn.setOnClickListener(this);

        sendTv = (TextView) findViewById(R.id.tv_send);
        receiveTv = (TextView) findViewById(R.id.tv_receive);

        onNextBtn = (Button) findViewById(R.id.btn_subscribe_onNext);
        onNextBtn.setOnClickListener(this);

        onErrorBtn = (Button) findViewById(R.id.btn_subscribe_onError);
        onErrorBtn.setOnClickListener(this);
    }

    private void initData() {
        sendBuilderString = new StringBuilder("发送的数据:\n");
        sendTv.setText(sendBuilderString.toString());
        receiveBuilderString = new StringBuilder("接受的数据：\n");
        receiveTv.setText(receiveBuilderString.toString());
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_subscribe_no_param:
                subscribeNoParams();
                break;
            case R.id.btn_subscribe_onNext:
                subscribeOnNext();
                break;
            case R.id.btn_subscribe_onError:
                subscribeOnError();
                break;
        }
    }

    //不带任何参数的subscribe() 表示下游不关心任何事件,你上游尽管发你的数据去吧, 老子可不管你发什么.
    private void subscribeNoParams() {
        sendBuilderString = null;
        receiveBuilderString = null;
        initData();
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                sendBuilderString.append("---->" + 1 + "\n");
                emitter.onNext(2);
                sendBuilderString.append("---->" + 2 + "\n");
                emitter.onNext(3);
                sendBuilderString.append("---->" + 3 + "\n");
                emitter.onComplete();
                sendBuilderString.append("---->" + "onComplete\n");
                sendTv.setText(sendBuilderString.toString());
            }
        }).subscribe();
    }


    //一个参数subscribe(Consumer<? super T> onNext)
    //带有一个Consumer参数的方法表示下游只关心onNext事件, 其他的事件我假装没看见
    private void subscribeOnNext() {
        sendBuilderString = null;
        receiveBuilderString = null;
        initData();
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                sendBuilderString.append("---->" + 1 + "\n");
                emitter.onNext(2);
                sendBuilderString.append("---->" + 2 + "\n");
                emitter.onNext(3);
                sendBuilderString.append("---->" + 3 + "\n");
                emitter.onComplete();
                sendBuilderString.append("---->" + "onComplete\n");
                sendTv.setText(sendBuilderString.toString());
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                receiveBuilderString.append("---->" + integer);
                receiveTv.setText(receiveBuilderString.toString());
            }
        });
    }


    private void subscribeOnError() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                sendBuilderString.append("---->" + 1 + "\n");
                emitter.onNext(2);
                sendBuilderString.append("---->" + 2 + "\n");
                emitter.onNext(3);
                sendBuilderString.append("---->" + 3 + "\n");
                emitter.onError(new Throwable("抛出异常onError"));
                sendBuilderString.append("---->" + "抛出异常onError\n");
                sendTv.setText(sendBuilderString.toString());
            }
        }).subscribe(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) throws Exception {
                receiveBuilderString.append("---->" + integer + "\n");
                receiveTv.setText(receiveBuilderString.toString());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                receiveBuilderString.append("---->" + throwable.getLocalizedMessage());
                receiveTv.setText(receiveBuilderString.toString());
            }
        });
    }

}
